<?php
(!defined('IN_SYSTEM') || !defined('ADMIN_PRE')) && exit('Access Denied!');
$f = isset($_REQUEST['f'])?$_REQUEST['f']:'index';
if(!in_array($f,array('index','add','edit','save','delete'))){
	exit('Access Denied!');
}
if(isset($_G['usergroup']['access']["usergroup$f"]) && !ican("usergroup$f")){
	msg('没有权限');
}
$m = new UsergroupModule;
$m->$f();
class UsergroupModule
{
	function index() {
		global $_G;
		$wheresql = '';
		if ( $keyword = getGP('keyword','G') ) {
			$wheresql .= " AND groupname LIKE '%$keyword%'";
			$url .= '&keyword='.rawurlencode($keyword);
		}
		$sql = "SELECT * FROM ".DB_PREFIX."usergroups WHERE 1 $wheresql";
		$query = $_G['db']->query($sql);
		$result = array();
		while ($row = $_G['db']->fetch_array($query)) {
			$row['usercount'] = $_G['db']->result("SELECT COUNT(*) AS num FROM ".DB_PREFIX."users WHERE groupid = '$row[gid]'");
			$result[] = $row;
		}
		include admintemplate('usergroup');
	}
	function add(){
		global $_G;
		$perms=array(
		'前台评论权限'=>array('post_comment'=>'发表评论','reply_comment'=>'回复评论','comneedcheck'=>'评论需要审核','comneedcode'=>'评论需要验证码','comviewhide'=> '查看隐藏评论'),
		'文章管理权限'=>array('artindex'=>'查看自己的文章','artedit'=>'编辑自己的文章','artindexall'=>'查看他人的文章','arteditall'=>'编辑他人的文章','artwrite'=>'新增文章','artwritecheck'=>'新文章需审核','artcheck'=>'审核新文章','artpromote'=>'置顶文章','artdelete'=>'删除自己的文章','artdeleteall'=>'删除他人的文章','artviewhide'=> '查看隐藏文章'),
		'分类管理权限'=>array('catindex'=>'查看分类','catmerge'=>'合并分类','catedit'=>'修改分类','catadd'=>'添加分类','catdel'=>'删除分类'),
		'评论管理权限'=>array('comindex'=>'查看评论','comcheck'=>'审核评论','comajax_edit'=>'修改评论','comdelete'=>'删除评论'),
		'标签管理权限'=>array('tag'=>'管理标签'),
		'页面管理权限'=>array('page'=>'管理页面'),
		'电子商务'=>array('ecsetting'=>'基本设置','ecpayment'=>'支付方式管理'),
		'订单管理权限'=>array('ecolist'=>'查看订单','ecdetail'=>'查看订单详情','ecedit'=>'修改订单','ecdelete'=>'删除订单'),
		'店铺管理权限'=>array('shopindex'=>'查看店铺列表','shopedit'=>'修改店铺','shopajax_check'=>'审核店铺','shopdelete'=>'删除店铺'),
		'导航管理权限'=>array('navindex'=>'查看导航','navedit'=>'修改导航','navadd'=>'新增导航','navdelete'=>'删除导航'),
		'用户管理权限'=>array('userindex'=>'查看用户','userajax_check_user'=>'审核新用户','useredit'=>'修改用户资料','useradd'=>'新增用户','userdelete'=>'删除用户','userpromote'=>'更改用户所属组'),
		'用户组管理权限'=>array('usergroupindex'=>'查看用户组','usergroupedit'=>'修改用户组','usergroupadd'=>'新增用户组','usergroupdelete'=>'删除用户组'),
		'系统管理'=>array('sysconfig'=>'网站设置','systheme'=>'主题管理','sysdata'=>'数据维护','syscreatehtml'=>'静态生成','sysplugin'=>'插件管理','sysattachment'=>'附件管理','region'=>'地区设置'),
		);
		include admintemplate('usergroup_add');
	}
	function edit(){
		global $_G;
		$gid = getGP('id','G','int');
		if(isset($_G['usergroups'][$gid])) {
			$usergroup = $_G['usergroups'][$gid] ;
		}else{
			$usergroup = $_G['db']->fetch_one_array("SELECT * FROM ".DB_PREFIX."usergroups WHERE gid = '$gid'");
			$_G['usergroups'][$usergroup['gid']] = $usergroup;
		}
		$access = unserialize($usergroup['access']);
		$perms=array(
		'前台评论权限'=>array('post_comment'=>'发表评论','reply_comment'=>'回复评论','comneedcheck'=>'评论需要审核','comneedcode'=>'评论需要验证码','comviewhide'=> '查看隐藏评论'),
		'文章管理权限'=>array('artindex'=>'查看自己的文章','artindexall'=>'查看他人的文章','artedit'=>'编辑自己的文章','arteditall'=>'编辑他人的文章','artwrite'=>'新增文章','artwritecheck'=>'新文章需审核','artcheck'=>'审核新文章','artpromote'=>'置顶文章','artdelete'=>'删除自己的文章','artdeleteall'=>'删除他人的文章','artviewhide'=> '查看隐藏文章'),
		'分类管理权限'=>array('catindex'=>'查看分类','catmerge'=>'合并分类','catedit'=>'修改分类','catadd'=>'添加分类','catdel'=>'删除分类'),
		'评论管理权限'=>array('comindex'=>'查看评论','comcheck'=>'审核评论','comajax_edit'=>'修改评论','comdelete'=>'删除评论'),
		'标签管理权限'=>array('tag'=>'管理标签'),
		'页面管理权限'=>array('page'=>'管理页面'),
		'电子商务'=>array('ecsetting'=>'基本设置','ecpayment'=>'支付方式管理'),
		'订单管理权限'=>array('ecolist'=>'查看订单','ecdetail'=>'查看订单详情','ecedit'=>'修改订单','ecdelete'=>'删除订单'),
		'店铺管理权限'=>array('shopindex'=>'查看店铺','shopedit'=>'修改店铺','shopajax_check'=>'审核店铺','shopdelete'=>'删除店铺'),
		'导航管理权限'=>array('navindex'=>'查看导航','navedit'=>'修改导航','navadd'=>'新增导航','navdelete'=>'删除导航'),
		'用户管理权限'=>array('userindex'=>'查看用户','userajax_check_user'=>'审核新用户','useredit'=>'修改用户资料','useradd'=>'新增用户','userdelete'=>'删除用户','userpromote'=>'更改用户所属组'),
		'用户组管理权限'=>array('usergroupindex'=>'查看用户组','usergroupedit'=>'修改用户组','usergroupadd'=>'新增用户组','usergroupdelete'=>'删除用户组'),
		'积分管理权限'=>array('potsetting'=>'积分策略','potedit'=>'调整积分策略','potindex'=>'基本设置','potlog'=>'积分记录'),
		'系统管理'=>array('sysconfig'=>'网站设置','sysview'=>'查看系统信息','systheme'=>'主题管理','sysdata'=>'数据维护','syscreatehtml'=>'静态生成','sysplugin'=>'插件管理','sysattachment'=>'附件管理'),
		);
		include admintemplate('usergroup_edit');
	}
	function save(){
		if(!ican("usergroupedit") && !ican("usergroupadd")){
			msg('没有权限');
		}
		global $_G;
		$gid = getGP('gid','P','int');
		$savetype = getGP('savetype','P');
		$name = getGP('groupname','P');
		$point = getGP('point','P','int');
		$accessex = getGP('accessex','P','array');
		$usergroup = array(
			'groupname' => $name,
			'point' => $point,
			'access' => serialize($accessex)
		);
		if ($savetype == 'new') {
			$_G['db']->insert('usergroups',$usergroup);
			recache('usergroups');
			msg('成功添加一个用户组','admin.php?m=usergroup');
		} elseif ($savetype == 'edit') {
			$_G['db']->update('usergroups',$usergroup, array('gid' => $gid));
			recache('usergroups');
			msg('成功编辑一个用户组','admin.php?m=usergroup');
		}
	}
	function delete(){
		global $_G;
		$idarr = getGP('id','P','array');
		if (count($idarr)) {
			foreach ($idarr as $gid) {
				$_G['db']->query("DELETE FROM ".DB_PREFIX."usergroups WHERE `type` != 'system' AND gid = '$gid'");
				$_G['db']->query("UPDATE ".DB_PREFIX."users SET groupid = 2 WHERE groupid = '$gid'");
			}
		} else {
			msg('请选择要删除的用户组');
		}
		recache('usergroups');
		msg('成功删除选中的用户组','admin.php?m=usergroup');
	}
	
}